﻿using DataAccess;
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
   public class ChiTietPhieuXuatDAO
    {
       public static String TaoMaChiTietPhieuXuat()
       {
           String strChu = "CTPX";
           String strSo = "0";
           String strMaDau = "CTPX001";

           String strSql = "SELECT TOP 1 MaCTPhieuXuat FROM CHITIETPHIEUXUAT ORDER BY MaCTPhieuXuat DESC";

           try
           {
               String strMaCuoi = DataConnection.ExecScalar(strSql).ToString();
               String strTemp = ((int.Parse(strMaCuoi.Remove(0, 4)) + 1)).ToString();
               String strMaDaiLy = strChu;
               while (strMaDaiLy.Length + strTemp.Length < 7)
                   strMaDaiLy += strSo;
               return strMaDaiLy + strTemp;

           }
           catch (Exception)
           {
               return strMaDau;
           }
       }

       public static int ThemChiTietPhieuXuat(ChiTietPhieuXuatDTO nv)
       {

           string query = "INSERT INTO CHITIETPHIEUXUAT (MaCTPhieuXuat,MaHang,DonViTinh,DonGia,SLSanXuat,ThanhTien,PhieuXuat)";
           query += "VALUES('" + nv.MaChiTietPhieuXuat + "','" + nv.MaMatHang + "','" + nv.MaDonViTinh + "','" + nv.DonGia + "','" + nv.SoLuongXuat + "','" + nv.ThanhTien + "','" + nv.MaPhieuXuat + "')";
           try
           {
               DataConnection.ExecQuery(query);
               return 1;
           }
           catch (System.Exception e)
           {
               Console.WriteLine("Not access to database:{0}", e.Message);
               return 0;
           }
       }


       public static List<ChiTietPhieuXuatDTO> LoadDanhSachPXHIenTai(string MaPhieuXuat)
       {
           List<ChiTietPhieuXuatDTO> list = new List<ChiTietPhieuXuatDTO>();
           string query = "SELECT c.TenMatHang,b.TenDonViTinh,a.DonGia,a.SLSanXuat,a.ThanhTien,a.MaCTPhieuXuat FROM CHITIETPHIEUXUAT a JOIN DONVITINH b ON a.DonViTinh = b.MaDonViTinh JOIN MATHANG c ON a.MaHang = c.MaMatHang where PhieuXuat='" + MaPhieuXuat + "'";
           DataTable dt = DataConnection.ExecQuery(query);
           if (dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   DataRow dr = dt.Rows[i];
                   ChiTietPhieuXuatDTO dl = new ChiTietPhieuXuatDTO();
                  
                   dl.TenMatHang = dr["TenMatHang"].ToString();
                   dl.TenDonViTinh = dr["TenDonViTinh"].ToString();
                   dl.DonGia = dr["DonGia"].GetHashCode();
                   dl.SoLuongXuat = dr["SLSanXuat"].GetHashCode();
                   dl.ThanhTien = dr["ThanhTien"].GetHashCode();
                   dl.MaChiTietPhieuXuat = dr["MaCTPhieuXuat"].ToString();
                   list.Add(dl);
               }
           }
           return list;
       }

       public static bool deleteListCTPX(string MaPhieuXuat)
       {
           string query = "DELETE CHITIETPHIEUXUAT WHERE PhieuXuat = '" + MaPhieuXuat + "'";
           try
           {
               DataConnection.ExecQuery(query);
               return true;
           }
           catch (System.Exception e)
           {
               Console.WriteLine("Not access to database:{0}", e.Message);
               return false;
           }
       }


       public static bool deleteChiTietPhieuXuat(string MaChiTietPhieuXuat)
       {
           string query = "DELETE CHITIETPHIEUXUAT WHERE MaCTPhieuXuat = '" + MaChiTietPhieuXuat + "'";
           try
           {
               DataConnection.ExecQuery(query);
               return true;
           }
           catch (System.Exception e)
           {
               Console.WriteLine("Not access to database:{0}", e.Message);
               return false;
           }
       }

       public static bool capNhatChiTietPhieuXuat(ChiTietPhieuXuatDTO ctpx)
       {
           string query = "UPDATE CHITIETPHIEUXUAT SET MaHang =" + ctpx.MaMatHang + ",DonViTinh='"+ctpx.MaDonViTinh+"' WHERE MaDaiLy =" + ctpx.MaChiTietPhieuXuat + "";
           try
           {
               DataConnection.ExecQuery(query);
               return true;
           }
           catch (System.Exception e)
           {
               Console.WriteLine("Not access database", e.Message);
               return false;
           }
       }
    }
}
